<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class IndexCollect extends BaseModel
{
    //定义表名 文章表
    protected $name = 'longbing_card_decorate_collect';

    protected $append=[
      'info_text'
    ];

    /**
     * User: chenniang
     * Date: 2019-10-22 17:15
     * @param $value
     * @param $data
     * @return array
     * descption:获取案列|工地|攻略详情
     */
    public function getInfoTextAttr($value,$data){
        if(in_array($data['type'],[1,2,3])){
            $model = $this->actionModel($data['type']);
            $dis[] = ['id','=',$data['news_id']];
            $info  = $model->where($dis)->find();
            return !empty($info)?$info->toArray():[];
        }
    }

    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '=', $status);
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 收藏列表
     */
    public function collectList($dis,$page = 10){
        $data = $this->where($dis)->status(1)->order('update_time desc')->paginate($page)->toArray();
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-10-18 14:57
     * @return void
     * descption:添加收藏
     */
    public function collectAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑收藏
     */
    public function collectUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;
    }
    /**
     * @param $dis
     * @return AdminCate
     * 收藏详情
     */
    public function collectInfo($dis){
        $data = $this->where($dis)->find();
        return !empty($data)?$data->toArray():$data;
    }
    /**
     * User: chenniang
     * Date: 2019-10-18 15:12
     * @return void
     * descption:收藏数量
     */
    public function collectCount($dis){
        $data = $this->where($dis)->count();
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-10-22 17:12
     * @return IndexCase|IndexNews|IndexSite
     * descption:获取model
     */
    public function actionModel($type){

        switch ($type){
            case 1:
                $model = new IndexCase();
                break;
            case 2:
                $model = new IndexSite();
                break;
            case 3:
                $model = new IndexNews();
                break;
        }
        return $model;
    }

    /**
     * User: chenniang
     * Date: 2019-10-23 09:50
     * @param $user_id
     * @param $id
     * @param $type
     * @return void
     * descption:取消收藏
     */

    public function collectDel($user_id,$id,$type){
        $c_dis[] = ['user_id','=',$user_id];
        $c_dis[] = ['news_id','=',$id];
        $c_dis[] = ['type','=',$type];
        $c_dis[] = ['status','=',1];
        $collect = $this->collectInfo($c_dis);
        if(!empty($collect)){
            $this->collectUpdate(['id'=>$collect['id']],['status'=>0]);
        }
        return true;
    }

}